package com.aaa.controller.front;

import com.aaa.entity.*;
import com.aaa.service.front.IShoppingCartDetailService;
import com.aaa.service.front.IShoppingCartService;
import com.aaa.service.front.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/front")
@Controller
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private IShoppingCartService shoppingCartService;
    @Resource
    private IShoppingCartDetailService shoppingCartDetailService;

    /**
     * 生成订单和订单明细
     * @return
     */
    @RequestMapping("/salesSubmit")
    public String salesSubmit(HttpSession session, Sales sales,Integer shoppingCartId){

        Double totalAmount = (Double) session.getAttribute("totalAmount");

        FrontUserinfo userinfo = (FrontUserinfo) session.getAttribute("userinfo");
        Integer UserInfoId_1 = userinfo.getUserInfoId();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = sdf.format(new Date());
        String salesId = "KU"+strDate;

        sales.setSalesId(salesId);
        sales.setBusinessDate(new Date());
        sales.setSalesAmount(totalAmount);
        sales.setUserInfoId(UserInfoId_1);
        orderService.addSales(sales);

        FrontUserinfo userInfo =(FrontUserinfo) session.getAttribute("userinfo");
        ShoppingCart cart = shoppingCartService.findCartUserInfoId(userInfo.getUserInfoId());
        List<ShoppingCartDetail> userInfoCartDetail = shoppingCartDetailService.findShoppingCartDetail(cart.getShoppingCartId());

        for(ShoppingCartDetail shoppingCartDetail : userInfoCartDetail){

            SalesDetails salesDetails = new SalesDetails();
            salesDetails.setSalesId(salesId);
            salesDetails.setGoodsId(shoppingCartDetail.getGoodsId());
            salesDetails.setGoodsCount(shoppingCartDetail.getGoodsCount());
            salesDetails.setTotalAmount(shoppingCartDetail.getGoodsAmount());

            orderService.addSalesDetails(salesDetails);

        }

        orderService.delShoppingCartDetail(cart.getShoppingCartId());

        return "front/order-tracking";
    }

    /**
     * 进行支付，修改客户订单状态
     * @param sales
     * @return
     */
    @RequestMapping("/editOrderState")
    public String editOrderState(Sales sales){
        orderService.editOrderState(sales);
        return "redirect:/front/myaccount";
    }

//    /**
//     * 根据购物车编号删除购物车明细
//     * @param shoppingCartId
//     * @return
//     */
//    @RequestMapping("/delShoppingCartDetail")
//    public String delShoppingCartDetail(Integer shoppingCartId){
//        orderService.delShoppingCartDetail(shoppingCartId);
//        return "front/order-tracking";
//    }
}
